<?php

if($_POST["submit"] == 'get xpath')
{
    $sourceurl = $_POST['sourceurl'];
    $sitemap_meta_id = $_POST['sitemap_meta_id'];
    $_POST = services::get_content_xpath_as_post($sitemap_meta_id);
    $_POST['sourceurl'] = $sourceurl;
    $_POST['sitemap_meta_id'] = $sitemap_meta_id;
}
?>
<html>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <SCRIPT language="javascript">
        $(function() {
            $('select[value]').each(function() {
                $(this).val(this.getAttribute("value"));
            });
        });
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[1].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                        newcell.childNodes[0].value = "";
                        break;
                    case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                    case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
                }
            }
        }

        function deleteRow(tableID) {
            try {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;

                for(var i=0; i<rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if(null != chkbox && true == chkbox.checked) {
                        if(rowCount <= 1) {
                            alert("Cannot delete all the rows.");
                            break;
                        }
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }


                }
            }catch(e) {
                alert(e);
            }
        }

    </SCRIPT>
</HEAD>
<BODY>

<form method="post">
    <?php
    if($_POST != null)
    {
        $sitemap_meta_id = $_POST['sitemap_meta_id'];
        $sourceurl = $_POST['sourceurl'];
        $title = $_POST['title'];
        $type = $_POST['type'];
        $multi = $_POST['multi'];
        $xpath = $_POST['xpath'];
        $attribute = $_POST['attribute'];
        $asis = $_POST['asis'];
        $place_holder = $_POST['place_holder'];
    }
    ?>
    Sitemap Meta ID: <input name="sitemap_meta_id" type="text" value="<?=$sitemap_meta_id?>" size="3" /><input type="submit" name="submit" value="get xpath" /><br/>
    Source Url: <input name="sourceurl" type="text" value="<?=$sourceurl?>" size="80" />
    <TABLE id="dataTable" width="350px" border="1">
        <TR>
            <TD>Checked</TD>
            <TD>Title</TD>
            <TD>Type</TD>
            <TD>Multi</TD>
            <TD>Xpath</TD>
            <TD>Attribute</TD>
            <TD>As IS</TD>
            <td>place holder</td>
        </TR>

        <?php
        if($_POST != null)
        {
            foreach($title as $a => $b)
            {
                ?>
                <TR>
                    <TD><INPUT type="checkbox" name="chk[]"/></TD>
                    <TD><INPUT type="text" name="title[]" value="<?=$title[$a]?>"></TD>
                    <TD>
                        <select name="type[]" value="<?=$type[$a]?>">
                            <option value="img">Image</option>
                            <option value="p">Paragraph</option>
							<option value="ul">ul</option>
							<option value="div">div</option>
                        </select>
                    </TD>
                    <TD>
                        <select name="multi[]" value="<?=$multi[$a]?>">
                            <option value="0">False</option>
                            <option value="1">True</option>
                        </select>
                    </TD>
                    <TD><INPUT type="text" size="70" name="xpath[]" value="<?=escape_chars($xpath[$a])?>"></TD>
                    <TD><INPUT type="text" name="attribute[]" value="<?=$attribute[$a]?>"></TD>
                    <TD>
                        <select name="asis[]" value="<?=$asis[$a]?>">
                            <option value="0">False</option>
                            <option value="1">True</option>
                        </select>
                    </TD>
                    <td>
                        <select type="text" name="place_holder[]" value="<?=$place_holder[$a]?>">
                            <option value="a">a. Body</option>
                            <option value="b">b. Images</option>
                            <option value="c">c. Poster</option>
                            <option value="d">d. Below Poster</option>
                            <option value="e">e. Rating</option>
                            <option value="f">f. Votes</option>
                            <option value="g">g. Release Date</option>
                            <option value="h">h. Category</option>
                            <option value="i">i. Genre</option>
                            <option value="j">j. Description</option>
                            <option value="k">k. Similar</option>
                        </select>
                    </td>
                </TR>
                <?php
            }
        }
        else
        {
            ?>
            <TR>
                <TD><INPUT type="checkbox" name="chk[]"/></TD>
                <TD><INPUT type="text" name="title[]" value="<?=$title[$a]?>"></TD>
                <TD>
                    <select name="type[]" value="<?=$type[$a]?>">
                        <option value="img">Image</option>
                        <option value="p">Paragraph</option>
						<option value="ul">ul</option>
						<option value="div">div</option>
                    </select>
                </TD>
                <TD>
                    <select name="multi[]" value="<?=$multi[$a]?>">
                        <option value="0">False</option>
                        <option value="1">True</option>
                    </select>
                </TD>
                <TD><INPUT type="text" size="70" name="xpath[]" value="<?=$xpath[$a]?>"></TD>
                <TD><INPUT type="text" name="attribute[]" value="<?=$attribute[$a]?>"></TD>
                <TD>
                    <select name="asis[]" value="<?=$asis[$a]?>">
                        <option value="0">False</option>
                        <option value="1">True</option>
                    </select>
                </TD>
                <td>
                    <!--input type="text" name="place_holder[]" value="<?=$place_holder{$a}?>"-->
                    <select type="text" name="place_holder[]" value="<?=$place_holder[$a]?>">
                        <option value="a">a. Body</option>
                        <option value="b">b. Images</option>
                        <option value="c">c. Poster</option>
                        <option value="d">d. Below Poster</option>
                        <option value="e">e. Rating</option>
                        <option value="f">f. Votes</option>
                        <option value="g">g. Release Date</option>
                        <option value="h">h. Category</option>
                        <option value="i">i. Genre</option>
                        <option value="j">j. Description</option>
                        <option value="k">k. Similar</option>
                    </select>
                </td>
            </TR>
            <?php
        }
        ?>
    </TABLE>
    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
    <input type="submit" name="submit" value="preview" />
    <input type="submit" name="submit" value="submit" />

</form>

<?php
if($_POST != null)
{
    $sitemap_meta_id = $_POST['sitemap_meta_id'];
    $sourceurl = $_POST['sourceurl'];

    $title = $_POST['title'];
    $type = $_POST['type'];
    $multi = $_POST['multi'];
    $xpath = $_POST['xpath'];
    $attribute = $_POST['attribute'];
    $asis = $_POST['asis'];
    $place_holder = $_POST['place_holder'];

    /*
    echo $sitemap_meta_id.'<br />';
    echo $sourceurl.'<br />';

    foreach($title as $a => $b)
    {
        $xpath[$a] = stripslashes($xpath[$a]);
        echo "$title[$a] - $type[$a] - $multi[$a] - $xpath[$a] - $attribute[$a] - $asis[$a] - $place_holder[$a]  <br />";
    }
    */
    $primative =  primative::do_get_primative_data($_POST);
    echo $primative['output'];

    echo '<h2>poster</h2>'.$primative['poster'];
    echo '<h2>description</h2>'.$primative['description'];

    if($_POST["submit"] == 'submit')
    {
        services::insert_content_xpath($_POST);
        echo '<br />Added into Database...';
    }
}

function escape_chars($string)
{
    $string = stripslashes($string);
    $string = str_replace('\"', '&#34', $string);
    $string = str_replace('"', '&#34', $string);
    return $string;
}

?>

</BODY>
</html>

